/**
 * 
 */
package com.tvshows.front.view.dialog;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;

import com.tvshows.front.factory.TypeUI;
import com.tvshows.front.listener.dialog.DialogListener;
import com.tvshows.front.ui.dialog.DialogAddFavoriteUI;
import com.tvshows.front.view.labels.FrontButton;
import com.tvshows.front.view.labels.FrontLabel;
import com.tvshows.front.view.labels.FrontText;
import com.tvshows.interactions.ObserverBdd;
import com.tvshows.structure.bdd.BddQueries;
import com.tvshows.structure.beans.Favorite;
import com.tvshows.structure.beans.Schedule;

/**
 * @author tMidy
 * <p>
 * Dialog used to add a show to the favorite shows list
 * </p>
 */
public class DialogAddFavoriteView
    extends Composite
    implements DialogAddFavoriteUI
{

    /**
     * Information about the show
     */
    private Group informationGroup;

    /**
     * Panel containing ok, cancel buttons
     */
    private Group validationGroup;

    /**
     * The text fields
     */
    private FrontText nameText, seasonText, lastEpisodeText;

    /**
     * The ok button
     */
    private FrontButton okButton;

    /**
     * The cancel button
     */
    private FrontButton cancelButton;

    /**
     * The schedule list
     */
    private Combo comboWeekDays;

    /**
     * true if modifying an existing show
     */
    private boolean modify = false;

    /**
     * @param parent the parent composite
     * @param style style of the composite
     */
    public DialogAddFavoriteView( Composite parent, int style )
    {
        super( parent, style );
        this.getShell().setText( "Ajouter un favori" );
        initGroups();
        initButtons();
        initListener();
    }

    /**
     * Initialize the favorite information
     * 
     * @param favorite the favorite to initialize
     */
    private void initFields( Favorite favorite )
    {
        nameText.setText( favorite.getNameFavorite() );
        seasonText.setText( favorite.getSeasonNumber() );
        lastEpisodeText.setText( favorite.getLastEpisodeNumber() );
        if ( favorite.getScheduleFavorite() != null )
        {
            comboWeekDays.select( comboWeekDays.indexOf( favorite.getScheduleFavorite() ) );
        }
    }

    /**
     * Initializes the listener
     */
    private void initListener()
    {
        /*
         * The listener
         */
        DialogListener listener = new DialogListener( this );
        okButton.addListener( SWT.Selection, listener );
        cancelButton.addListener( SWT.Selection, listener );
    }

    /**
     * Initializes the groups
     */
    private void initGroups()
    {
        informationGroup = new Group( this, SWT.BORDER );
        informationGroup.setText( "Informations sur la série" );
        informationGroup.setBounds( 10, 10, 300, 160 );

        validationGroup = new Group( this, SWT.NONE );
        validationGroup.setLayout( new RowLayout( SWT.HORIZONTAL ) );
        validationGroup.setBounds( 10, 10 + 160, 300, 60 );

    }

    /**
     * Initializes the buttons
     */
    private void initButtons()
    {

        /*
         * The labels
         */
        FrontLabel nameLabel = new FrontLabel( informationGroup, SWT.NONE );
        nameLabel.setText( "Nom du favori : " );
        nameLabel.setBounds( 5, 30, 100, 25 );
        nameText = new FrontText( informationGroup, SWT.NONE );
        nameText.setBounds( 125, 30, 150, 20 );

        FrontLabel seasonLabel = new FrontLabel( informationGroup, SWT.NONE );
        seasonLabel.setText( "Saison en cours : " );
        seasonLabel.setBounds( 5, 60, 100, 25 );
        seasonText = new FrontText( informationGroup, SWT.NONE );
        seasonText.setBounds( 125, 60, 30, 20 );
        seasonText.setTextLimit( 2 );

        FrontLabel lastEpisodeLabel = new FrontLabel( informationGroup, SWT.NONE );
        lastEpisodeLabel.setText( "Dernier épisode : " );
        lastEpisodeLabel.setBounds( 5, 90, 100, 25 );
        lastEpisodeText = new FrontText( informationGroup, SWT.NONE );
        lastEpisodeText.setBounds( 125, 90, 30, 20 );
        lastEpisodeText.setTextLimit( 2 );

        FrontLabel comboWeekDaysLabel = new FrontLabel( informationGroup, SWT.NONE );
        comboWeekDaysLabel.setText( "Jour de passage : " );
        comboWeekDaysLabel.setBounds( 5, 120, 100, 25 );
        comboWeekDays = new Combo( informationGroup, SWT.READ_ONLY );
        comboWeekDays.setBounds( 125, 120, 100, 20 );
        comboWeekDays.setItems( Schedule.toStringValues() );

        okButton = new FrontButton( validationGroup, SWT.NONE );
        okButton.setText( "Valider" );
        okButton.setBounds( 45, 20, 100, 25 );
        cancelButton = new FrontButton( validationGroup, SWT.NONE );
        cancelButton.setText( "Annuler" );
        cancelButton.setBounds( 155, 20, 100, 25 );
    }

    /**
     * @return the okButton
     */
    public Button getOkButton()
    {
        return okButton;
    }

    /**
     * @return the cancelButton
     */
    public Button getCancelButton()
    {
        return cancelButton;
    }

    /**
     * @see com.tvshows.front.ui.dialog.DialogAddFavoriteUI#getTexts()
     */
    @Override
    public String[] getTexts()
    {
        String schedule = null;
        if ( comboWeekDays.getSelectionIndex() > -1 )
        {
            schedule = comboWeekDays.getItem( comboWeekDays.getSelectionIndex() );
        }
        return new String[] { nameText.getText(), seasonText.getText(), lastEpisodeText.getText(), schedule };
    }

    /**
     * @param modify modification or not
     */
    public void setModify( boolean modify )
    {
        this.modify = modify;
        if ( ObserverBdd.getObserverBdd().getListFavorite().isToModify() && modify )
        {
            Favorite favorite =
                BddQueries.getBddQueries().loadFavorite(
                    ObserverBdd.getObserverBdd().getListFavorite().getSelectedFavorite() );
            initFields( favorite );
            this.nameText.setEnabled( false );
        }
    }

    /**
     * @see com.tvshows.front.ui.dialog.DialogUI#getType()
     */
    @Override
    public TypeUI getType()
    {
        return TypeUI.ADD_FAVORITE;
    }

    /**
     * @return the modify
     */
    @Override
    public boolean isModify()
    {
        return modify;
    }

    @Override
    public void initFormData()
    {
        // unused
    }

    @Override
    public void initComposites()
    {
        // unused
    }

}
